package com.syt.order.controller.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.syt.enums.OrderStatusEnum;
import com.syt.model.order.OrderInfo;
import com.syt.order.service.OrderInfoService;
import com.syt.result.Result;
import com.syt.utils.AuthContextHolder;
import com.syt.vo.order.OrderCountQueryVo;
import com.syt.vo.order.OrderQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: xxx
 * @Date: 2023/2/26 20:36
 * @Description: 订单控制器
 */
@Api(tags = "订单管理接口")
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderApiController {

    @Autowired
    private OrderInfoService orderInfoService;

    @ApiOperation(value = "获取订单统计数据")
    @PostMapping("/inner/getCountMap")
    public Map<String, Object> getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo) {
        return orderInfoService.getOrderCountMap(orderCountQueryVo);
    }


    @ApiOperation(value = "取消预约")
    @GetMapping("/auth/cancelOrder/{orderId}")
    public Result cancelOrder(@PathVariable Long orderId){
       boolean isOrder=    orderInfoService.removeOrder(orderId);
       return Result.ok(isOrder);
    }


    @ApiOperation(value = "获取订单状态")
    @GetMapping("/auth/getStatusList")
    public Result getStatusList(){
        return Result.ok(OrderStatusEnum.getStatusList());
    }


    @ApiOperation(value = "订单列表(条件查询带分页)")
    @GetMapping("/auth/{page}/{limit}")
    public Result orderListPage(@PathVariable long page,
                                @PathVariable long limit,
                                OrderQueryVo orderQueryVo,
                                HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        orderQueryVo.setUserId(userId);
        Page<OrderInfo> pageParam = new Page<>(page, limit);
        Page<OrderInfo> pageMode = orderInfoService.getOrdersListOfPage(pageParam,orderQueryVo);
        return Result.ok(pageMode);
    }

    @ApiOperation(value = "根据id查询订单详情")
    @GetMapping("/auth/getOrdersById/{orderId}")
    public Result getOrdersById(@PathVariable Long orderId){
         OrderInfo orderInfo= orderInfoService.getOrdersById(orderId);
         return Result.ok(orderInfo);
    }

    @ApiOperation(value = "生成挂号订单")
    @PostMapping("/auth/submitOrder/{scheduleId}/{patientId}")
    public Result generateAnOrder(@PathVariable String scheduleId, @PathVariable Long patientId){
           Long orderId= orderInfoService.saveOrder(scheduleId, patientId);
           return Result.ok(orderId);
    }

}
